import {
  {{ smallName }}PageApi,
  add{{ bigName }}Api,
  edit{{ bigName }}Api,
  del{{ bigName }}Api
} from '@/api/{{apiPath}}/{{ fileName }}'
import { Ref } from 'vue'
import { ProTableInst } from '@/components/pro-table/type'
import { PageRequest } from '@/api/types'
import { {{ bigName }}Model } from '@/api/model/{{ fileName }}-model'

type Props = {
  handleType: Ref<'add' | 'edit' | ''>
  handleTag: Ref<string>
  {{ smallName }}{{ popType }}: Ref<boolean>
  {{ smallName }}Form: Ref<Partial<{{ bigName }}Model>>
  proTableInst: Ref<ProTableInst | null>
}

export const use{{ bigName }}Api = ({
  handleType,
  handleTag,
  {{ smallName }}{{ popType }},
  {{ smallName }}Form,
  proTableInst
}: Props) => {
  const confirmLoading = ref(false)

  // 获取{{ cname }}列表
  const fetch{{ bigName }}Page = async (
    pageRequest: PageRequest,
    queryParams: {{ bigName }}Model
  ) => {
    // const res = await {{ smallName }}PageApi({
    //   ...queryParams,
    //   pageRequest
    // })

    // if (res.code === '0000') {
    //   return {
    //     data: res.data?.content,
    //     total: res.data?.total
    //   }
    // }
    return {
      data: [
        {
          key1: '我是普通列',
          key2: '我能自定义列属性',
          key3: `https://www.baidu.com`,
          key4: 100,
          key5: 200
        },
        {
          key1: '我是普通列',
          key2: '我能自定义列属性',
          key3: `https://cn.vuejs.org`,
          key4: 200,
          key5: 200
        }
      ],
      total: 2
    }
  }

  // 新增or修改{{ cname }}
  const fetchAddOrEdit{{ bigName }} = async () => {
    confirmLoading.value = true
    const fetch =
      handleType.value === 'add' ? add{{ bigName }}Api : edit{{ bigName }}Api
    const res = await fetch({
      ...{{ smallName }}Form.value
    })
    if (res.code === '0000') {
      useMsg('success', handleTag.value + '成功')
      proTableInst.value?.reload()
      {{ smallName }}{{ popType }}.value = false
    }
    confirmLoading.value = false
  }

  // 删除{{ cname }}
  const fetchDel{{ bigName }} = async (id) => {
    const res = await del{{ bigName }}Api({ id })
    if (res.code === '0000') {
      useMsg('success', '删除成功')
      proTableInst.value?.reload()
    }
  }

  return {
    fetch{{ bigName }}Page,
    fetchAddOrEdit{{ bigName }},
    fetchDel{{ bigName }},
    confirmLoading
  }
}
